package fr.mywiki.model.jdbcAdapter.utils;

import java.util.Arrays;
import java.util.Iterator;

import fr.mywiki.model.jdbcAdapter.values.AbstractValueObject;


/**
 * This class contains several functions dealing with the ValueObject Objects and the manipulation of
 * their fields when exporting them to the database.
 * 
 * @author tlombard
 */
public class ExportValueObjectManager {

	/**
	 * <p>
	 * Creates and returns a String object corresponding to the list of all the field names of the
	 * given value object and their values , names between "`", values between "'" and every couple
	 * separated by a comma (",").
	 * </p>
	 * <p>
	 * The order of appearance of those fields is given by the variable FIELD-NAMES.
	 * </p>
	 * 
	 * @param value a AbstractValueObject object
	 * @return a String object with different field equalities separated by a comma.
	 */
	public static String getFieldEqualitiesList(AbstractValueObject value) {
		StringBuffer stringBuffer = new StringBuffer();
		Iterator itFields = Arrays.asList(value.getFieldNames()).iterator();
		String field;
		if (itFields.hasNext()) {
			 field= (String)itFields.next();
			stringBuffer.append("`" + field+"` = '"+value.get(field)+"'");
		}
		while (itFields.hasNext()) {
			field= (String)itFields.next();
			stringBuffer.append(", `" + field+"` = '"+value.get(field)+"'");
		}
		return stringBuffer.toString();
	}
	
	/**
	 * <p>
	 * Creates and returns a String object corresponding to the list of all the field names of the
	 * given value object, all between "`" and separated by a comma (",").
	 * </p>
	 * <p>
	 * The order of appearance of those fields is given by the variable FIELD-NAMES.
	 * </p>
	 * 
	 * @param value a AbstractValueObject object
	 * @return a String object with different field names separated by a comma.
	 */
	public static String getFieldNamesList(AbstractValueObject value) {
		StringBuffer stringBuffer = new StringBuffer();
		Iterator itFields = Arrays.asList(value.getFieldNames()).iterator();
		if (itFields.hasNext())
			stringBuffer.append("`" + itFields.next()+"`");
		while (itFields.hasNext())
			stringBuffer.append(", `" + itFields.next()+"`");
		return stringBuffer.toString();
	}

	/**
	 * <p>
	 * Creates and returns a String object corresponding to the list of all the field values of the
	 * given value object, all between "'" and separated by a comma (",").
	 * </p>
	 * <p>
	 * The order of appearance of those values is given by the variable FIELD-NAMES.
	 * </p>
	 * 
	 * @param value a AbstractValueObject object
	 * @return a String object with different field names separated by a comma.
	 */
	public static String getFieldValuesList(AbstractValueObject value) {
		StringBuffer stringBuffer = new StringBuffer();
		Iterator itFields = Arrays.asList(value.getFieldNames()).iterator();
		if (itFields.hasNext())
			stringBuffer.append("'"+value.get((String) itFields.next())+"'");
		while (itFields.hasNext())
			stringBuffer.append(", '" + value.get((String) itFields.next())+"'");
		return stringBuffer.toString();
	}
}
